/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javamail.sip;

import java.util.List;
import java.util.Random;
import java.util.Vector;
import javamail.sdp.sdpMessage;
import javamail.sip.util.eSipHeader;
import javamail.sip.util.eSipMethodType;
import javamail.sip.util.eSipRespCode;

/**
 *
 * @author sriram
 */
public class sipMessage {
public boolean isRequest = false;
public eSipMethodType method = eSipMethodType.INVITE;
public eSipRespCode status = eSipRespCode.NIL;
public String[] headers = null;
public List<String> body = null;
public String hostname = null;
public int port = 0;
private Random rand = new Random();

    public sipMessage() {
        headers = new String [eSipHeader.values().length];
        body = new Vector<String>(1);
        hostname = new String("localhost");
    }
    
    public String getCSeq() {
        String cseqHeader = headers[eSipHeader.CSEQ.toInt()];
        if(cseqHeader != null)
            return cseqHeader;
        else
            return rand.nextInt(1000)+" "+method.toString();
    }
    
    public String getFrom() {
        return headers[eSipHeader.FROM.toInt()];
    }
    public String getTo() {
        return headers[eSipHeader.TO.toInt()];
    }
    public String getVia() {
        return headers[eSipHeader.VIA.toInt()];
    }
    public String getContact() {
        return headers[eSipHeader.CONTACT.toInt()];
    }

    public String getViaAddress() {
        String toHeader = headers[eSipHeader.VIA.toInt()];
        String[] splitLine = toHeader.split("\r\n");
        toHeader = splitLine[0];
        if(toHeader == null)
            return null;
        int firstbrkt = toHeader.indexOf("<");
        int lastbrkt = toHeader.lastIndexOf(">");
        // Return just the from address by removing the brackets.
        if(lastbrkt>0 && (lastbrkt>firstbrkt))
            return toHeader.substring(firstbrkt+1, lastbrkt);
        else {//Probably only the sip address was sent
            splitLine = toHeader.split(";");
            String[] sp = splitLine[0].split(" ");
            if(sp.length>1)
                return sp[1];
            else
                return splitLine[0];
        }
    }

    public String getViaHostOnly() {
        String toHeader = headers[eSipHeader.VIA.toInt()];
        String[] splitLine = toHeader.split("\r\n");
        toHeader = splitLine[0];
        if(toHeader == null)
            return null;
        int firstbrkt = toHeader.indexOf("<");
        int lastbrkt = toHeader.lastIndexOf(">");
        // Return just the from address by removing the brackets.
        if(lastbrkt>0 && (lastbrkt>firstbrkt))
            return toHeader.substring(firstbrkt+1, lastbrkt);
        else {//Probably only the sip address was sent
            splitLine = toHeader.split(";");
            String[] sp = splitLine[0].split(" ");
            if(sp.length>1)
                toHeader = sp[1];
            else
                toHeader = splitLine[0];
        }
        if(toHeader == null)
            return null;
        int portIndex = toHeader.indexOf(":");
        if(portIndex>0) {
            return toHeader.substring(0, portIndex);
        }
        return null;
    }

    public String getFromAddress() {
        String fromHeader = headers[eSipHeader.FROM.toInt()];
        if(fromHeader == null)
            return null;
        int firstbrkt = fromHeader.indexOf("<");
        int lastbrkt = fromHeader.lastIndexOf(">");
        // Return just the from address by removing the brackets.
        if(lastbrkt>0 && (lastbrkt>firstbrkt))
            return fromHeader.substring(firstbrkt+1, lastbrkt);
        else //Probably only the sip address was sent
            return fromHeader;
    }
    
    public String getToAddress() {
        String toHeader = headers[eSipHeader.TO.toInt()];
        if(toHeader == null)
            return null;
        int firstbrkt = toHeader.indexOf("<");
        int lastbrkt = toHeader.lastIndexOf(">");
        // Return just the from address by removing the brackets.
        if(lastbrkt>0 && (lastbrkt>firstbrkt))
            return toHeader.substring(firstbrkt+1, lastbrkt);
        else //Probably only the sip address was sent
            return toHeader;
    }
    
    public String getToUser() {
        String toHeader = headers[eSipHeader.TO.toInt()];
        String uri = null;
        if(toHeader == null)
            return null;
        int firstbrkt = toHeader.indexOf("<");
        int lastbrkt = toHeader.lastIndexOf(">");
        // Return just the from address by removing the brackets.
        if(lastbrkt>0 && (lastbrkt>firstbrkt))
            uri = toHeader.substring(firstbrkt+1, lastbrkt);
        else //Probably only the sip address was sent
            uri = toHeader;
        String sip = "sip:";
        int sipIndex = uri.indexOf(sip);
        if(sipIndex>=0)
            uri = uri.substring(sipIndex+sip.length());
        int atIndex = uri.indexOf("@");
        if(atIndex>=0)
            return uri.substring(0,atIndex);
        else
            return null;
    }
    
    public String getContactAddress() {
        String contactHeader = headers[eSipHeader.CONTACT.toInt()];
        if(contactHeader == null)
            return null;
        int firstbrkt = contactHeader.indexOf("<");
        int lastbrkt = contactHeader.lastIndexOf(">");
        // Return just the from address by removing the brackets.
        if(lastbrkt>0 && (lastbrkt>firstbrkt+1))
            return contactHeader.substring(firstbrkt+1, lastbrkt);
        else //Probably only the sip address was sent
            return contactHeader;
    }
    
    public String[] getHostNamePort() {
        String[] result = null;
        String name = null;
        if((name = getViaAddress()) !=null ||
                (name = getContactAddress()) !=null ||
                (name = getFromAddress()) != null) {
            if(name.startsWith("sip:"))
                name = name.substring(4);
            result = name.split(":");
            if(result.length > 2 || result.length < 1)
                return null;
            else if(result.length == 1)
                return result;
            else {
                try {
                    Integer.parseInt(result[1]);
                } catch (NumberFormatException ignored) { result[1] = "5060";}
            }
        }
        return result;
    }
    
    public String getCallid() {
        return headers[eSipHeader.CALLID.toInt()];
    }
    
    public void addSDP(sdpMessage sdpMsg) {
        String sdp = sdpMsg.toString();
        if(sdp == null)
            return;
        String[] sdpLines = sdp.split("\r\n");
        for(int i=0; i<sdpLines.length; i++)
            body.add(sdpLines[i]);
    }
    
    public void addMessage(String message) {
        if(message!=null)
            body.add(message);
    }

    public boolean containsBody() {
        boolean result = false;
        if(body.listIterator().hasNext())
            result = true;
        return result;
    }
}
